package com.qf.industry.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qf.core.dto.PageDto;
import com.qf.core.vo.PageResult;
import com.qf.core.vo.R;
import com.qf.industry.entity.Industry;
import com.qf.industry.mapper.IndustryMapper;
import com.qf.industry.service.IndustryService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.util.Date;

/**
 * <p>
 * 行业表 服务实现类
 * </p>
 *
 * @author wu
 * @since 2023-10-27
 */
@Service
public class IndustryServiceImpl extends ServiceImpl<IndustryMapper, Industry> implements IndustryService {

    @Override
    @Transactional
    public R add(Industry industry) {
        if (industry == null){
            return R.fail();
        }
        String idStr = IdWorker.getIdStr();
        industry.setId(idStr);
        industry.setCreateTime(new Date());
        this.baseMapper.insert(industry);
        return R.ok();
    }

    @Override
    public PageResult<Industry> page(PageDto<Industry> pageDto) {

        //分页信息
        IPage<Industry> page = new Page<>(pageDto.getNow(),pageDto.getSize());

        //条件
        QueryWrapper<Industry> queryWrapper = new QueryWrapper<>();
        if (!StringUtils.isEmpty(pageDto.getWhere().getName())){
            queryWrapper.lambda().like(Industry::getName,pageDto.getWhere().getName());
        }

        IPage<Industry> pageInfo = this.baseMapper.selectPage(page, queryWrapper);

        //封装分页结果
        PageResult<Industry> pageResult = new PageResult<>();
        pageResult.setPages(pageInfo.getPages());
        pageResult.setTotal(pageInfo.getTotal());
        pageResult.setList(pageInfo.getRecords());

        return pageResult;
    }

    @Override
    public R deleteById(Industry industry) {
        if (industry == null) {
            return R.fail();
        }
        int i = this.baseMapper.deleteById(industry.getId());
        if (i != 1) {
            return R.fail();
        }
        return R.ok();
    }


}
